<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增卡种</a-button>
        <a-button type="primary" @click="handleCopy"> 复制卡种</a-button>
        <a-button type="primary" @click="handleOpenSetDrawer(selectedRow)" :disabled="!hasSelectedRows"> 权益机场设置 </a-button>
        <a-button type="primary" @click="handleVipCardServer(selectedRow)" :disabled="!hasSelectedRows">权益初始化设置</a-button>
        <a-button type="primary" @click="handleVipCardUseAirportCopy(selectedRow)" :disabled="!hasSelectedRows">复制卡种使用机场 </a-button>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
    <VipcardFormDrawer @register="registerDrawer" @success="handleSuccess" />
    <VipcardCopyDrawer @register="registerVipcardCopyDrawer" @success="handleSuccess" />
    <VipcardSetDrawer @register="registerVipcardSetDrawer"></VipcardSetDrawer>
    <VipcardServerDrawer @register="registerVipCardServerDrawer"></VipcardServerDrawer>
    <VipcardAgreementDrawer @register="registerVipcardAgreementDrawer"></VipcardAgreementDrawer>
    <VipcardCopyUseAirportDrawer @register="registerVipcardCopyUseAirportDrawer"></VipcardCopyUseAirportDrawer>
  </div>
</template>
<script lang="ts" setup name="vipCard">
import { ActionItem, BasicTable, TableAction } from '@apps/antd-admin/src/components/Table';
import { Api } from './data/api';
import { useDrawer } from '@apps/antd-admin/src/components/drawer';
import VipcardFormDrawer from './components/VipcardFormDrawer.vue';
import VipcardCopyDrawer from './components/VipcardCopyDrawer.vue';
import VipcardSetDrawer from './components/VipcardSetDrawer.vue';
import VipcardServerDrawer from './components/VipcardServerDrawer.vue';
import VipcardAgreementDrawer from './components/VipcardAgreementDrawer.vue';
import VipcardCopyUseAirportDrawer from './components/VipcardCopyUseAirportDrawer.vue';
import { columns, searchFormSchema } from './data/data';
import { useBaseTable } from '@apps/antd-admin/src/hooks';

const [registerDrawer, { openDrawer }] = useDrawer();
const [registerVipcardSetDrawer, { openDrawer: openVipcardSetDrawer }] = useDrawer();
const [registerVipcardCopyDrawer, { openDrawer: openVipcardCopyDrawer }] = useDrawer();
const [registerVipCardServerDrawer, { openDrawer: openVipCardServerDrawer }] = useDrawer();
const [registerVipcardAgreementDrawer, { openDrawer: openVipcardAgreementDrawer }] = useDrawer();
const [registerVipcardCopyUseAirportDrawer, { openDrawer: openVipcardCopyUseAirportDrawer }] = useDrawer();

const { prefixCls, tableContext } = useBaseTable({
  tableProps: {
    title: '会员卡种列表',
    listUrl: Api.page,
    delUrl: Api.remove,
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    showIndexColumn: true,
    clickToRowSelect: true,
    rowKey: 'vipcardId',
    actionColumn: {
      width: 150,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
    rowSelection: {
      type: 'radio',
    },
  },
});

//注册table数据
const [registerTable, { reload }, { rowSelection, selectedRow, hasSelectedRows }] = tableContext;

/**
 * 行内操作栏
 */
function getTableAction(record): ActionItem[] {
  return [
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
    },
    {
      label: '协议管理',
      onClick: handleVipcardAgreement.bind(null, record),
    },
  ];
}

function handleCreate() {
  openDrawer(true, {
    isUpdate: false,
  });
}

function handleCopy() {
  openVipcardCopyDrawer(true, {
    isUpdate: false,
  });
}

function handleEdit(record: Recordable) {
  openDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handleOpenSetDrawer(record) {
  openVipcardSetDrawer(true, {
    record,
    isUpdate: false,
  });
}

function handleVipCardServer(record: Recordable) {
  openVipCardServerDrawer(true, {
    record,
    isUpdate: false,
  });
}

function handleVipcardAgreement(record: Recordable) {
  openVipcardAgreementDrawer(true, {
    record,
    isUpdate: false,
  });
}

function handleVipCardUseAirportCopy(record: Recordable) {
  openVipcardCopyUseAirportDrawer(true, {
    record,
    isUpdate: true,
  });
}

function handleSuccess() {
  reload();
}
</script>
